package com.example.demo.leetcode.top;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * 无重复字符串长度解题
 */
public class Test3 {

    static Set<Character> set = new HashSet<Character>();

    public static int lengthOfLongestSubstring(String s) {

        if(s == null){
            return 0;
        }
        if(s.length() == 1){
            return 1;
        }

        int start = 0;
        int end = 1;
        int length = 0;
        while(start <= s.length() && end <=s.length()){
            String tempStr = s.substring(start, end);
            int tempLength = tempStr.length();
            if(!isDupletic(tempStr)){
                if(tempLength>length){
                    length = tempLength;
                }
                end++;
            }else{
                start++;
                if(start >= end){
                    end++;
                }
            }
        }
        return length;
    }

    public static boolean isDupletic(String s){

        for(Character c : s.toCharArray()){
            set.add(c);
        }
        if(set.size() == s.length()){
            set.clear();
            return false;
        }
        set.clear();
        return true;
    }


    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("au"));

    }

}
